﻿#region File Description
/*-----------------------------------------------------------------------------
 Author: Jose Antonio Gallego
 Date: 24/10/2011
 
 License: Ms-PL
 
 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 SOFTWARE.

 The example companies, organizations, products, domain names,
 e-mail addresses, logos, people, places, and events depicted
 herein are fictitious.  No association with any real company,
 organization, product, domain name, email address, logo, person,
 places, or events is intended or should be inferred.
-----------------------------------------------------------------------------*/
#endregion

using Microsoft.Phone.Scheduler;
using Microsoft.Phone.Shell;
using System;
using System.Windows;
using System.IO.IsolatedStorage;
using System.Collections;
using System.Linq;

namespace ZombsquareScheduledTaskAgent
{
    public class ScheduledAgent : ScheduledTaskAgent
    {
        /// <summary>
        /// Agent that runs a scheduled task
        /// </summary>
        /// <param name="task">
        /// The invoked task
        /// </param>
        /// <remarks>
        /// This method is called when a periodic or resource intensive task is invoked
        /// </remarks>
        protected override void OnInvoke(ScheduledTask task)
        {

            int newCount = 0;

            ShellTile TileToFind = ShellTile.ActiveTiles.First();

            if (TileToFind != null)
            {
                // Recuperamos el contador del Zbrex
                IsolatedStorageSettings.ApplicationSettings.TryGetValue<int>("zbrex", out newCount);
                newCount--;

                // if Count was not entered, then assume a value of 0
                if (newCount <= 0)
                {
                    newCount = 0;
                }

                IsolatedStorageSettings.ApplicationSettings["zbrex"] = newCount;

                StandardTileData NewTileData = new StandardTileData
                {
                    Title = "Zombsquare",
                    BackgroundImage = new Uri("Background.png", UriKind.Relative),
                    Count = newCount,
                    BackTitle = "Zbrex",
                    BackBackgroundImage = new Uri("BackgroundTile.jpg", UriKind.Relative),
                    BackContent = newCount <= 0 ? "Vas camino de ser un zombie" : "Estas sano... de momento"
                };

                // Update the Application Tile
                TileToFind.Update(NewTileData);


                NotifyComplete();
            }
        }
    }
}
